﻿//
// File: UpdateArt01.cs
//
// This sample program exercises the ID3.NET Library by updating MP3 cover art with folder.jpg.
//

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Id3;

namespace UpdateArt01
{
    class Program
    {
        static string fName = @"C:\TESTDIR\01 - TRACK 01.mp3";

        static void Main ()
        {
            string ffName =  Path.GetDirectoryName (fName) + Path.DirectorySeparatorChar + "folder.jpg";

            using (var mp3 = new Mp3File (fName, Mp3Permissions.ReadWrite))
            {
                Id3Tag tag = mp3.GetTag (Id3TagFamily.Version2x);

                Console.WriteLine ("Title: {0}", tag.Title.Value);
                Console.WriteLine ("Artist: {0}", tag.Artists);
                Console.WriteLine ("Album: {0}", tag.Album.Value);
                Console.WriteLine ("Pictures: {0}", tag.Pictures.Count);

                if (tag.Pictures.Count == 0)
                {
                    Console.WriteLine ("Adding artwork from folder.jpg");

                    // Show the general case of an intermediate memory stream.
                    var folderArt = Image.FromFile (ffName);
                    var picStream = new MemoryStream ();
                    folderArt.Save (picStream, ImageFormat.Jpeg);

                    var picFrame = new Id3.Frames.PictureFrame ();
                    picFrame.LoadImage (picStream);
                    tag.Pictures.Add (picFrame);

                    mp3.WriteTag (tag, WriteConflictAction.Replace);
                }
            }
        }
    }
}
